package com.decheng.servlet;

import com.decheng.pojo.Book;
import com.decheng.pojo.Cart;
import com.decheng.service.BookService;
import com.decheng.service.CartService;
import com.decheng.service.impl.BookServiceImpl;
import com.decheng.service.impl.CartServiceImpl;
import com.decheng.servlet.base.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 孔德成
 * @Slogan 致敬大师，致敬未来的你
 */
public class CartServlet extends BaseServlet {

    private CartService cartService = new CartServiceImpl();
    private BookService bookService = new BookServiceImpl();

    /**
     * 跳转到购物车页面
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void toCartPage(HttpServletRequest request, HttpServletResponse response) throws IOException {

        engine.processTemplate("cart/cart", request, response);
    }

    /**
     * 将商品添加到购物车中
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void addCartItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取请求参数的 id 值
        String id = request.getParameter("id");
        // 2. 根据 id 查询 book 的信息
        Book queryBook = new Book();
        queryBook.setBookId(Integer.valueOf(id));
        Book book = bookService.getBook(queryBook);
        // 3. 将购物项添加到购物车中
        cartService.addCartItem(request.getSession(), book);
        // 4. 跳转到首页
//        response.sendRedirect(request.getContextPath() + "/index");
    }

    /**
     * 清空购物车
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void cleanCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 从 session 域中移除购物车对象
        request.getSession().removeAttribute("cart");
        // 显示购物车页面
        engine.processTemplate("cart/cart", request, response);
    }

    /**
     * 购物车页面 "-" 号按钮
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void countDecrease(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取请求参数的 id 值
        String id = request.getParameter("id");
        // 2. 从 session 中获取购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        // 3. 调用购物车的减少购物项数量-1方法
        cart.itemCountDecrease(Integer.valueOf(id));
        // 4. 检查购物项数量，如果为0，则移除购物车
        if (cart.getTotalCount() == 0) {
            session.removeAttribute("cart");
        }
        // 5. 显示购物车页面
        engine.processTemplate("cart/cart", request, response);
    }

    /**
     * 购物车页面 "+" 号按钮
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void countIncrease(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取请求参数的 id 值
        String id = request.getParameter("id");
        // 2. 从 session 中获取购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        // 3. 调用购物车的减少购物项数量+1方法
        cart.itemCountIncrease(Integer.valueOf(id));
        // 4. 显示购物车页面
        engine.processTemplate("cart/cart", request, response);
    }


    /**
     * 购物车页面 删除购物项
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void removeCartItem(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取请求参数的 id 值
        String id = request.getParameter("id");
        // 2. 从 session 中获取购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        // 3. 调用购物车的删除购物项方法
        cart.removeCartItem(Integer.valueOf(id));
        // 4. 检查购物项数量，如果为0，则移除购物车
        if (cart.getTotalCount() == 0) {
            session.removeAttribute("cart");
        }
        // 5. 显示购物车页面
        engine.processTemplate("cart/cart", request, response);
    }

    /**
     * 购物车页面 文本框修改数量
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void updateCartItemCount(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 1. 获取请求参数的 id 值
        String id = request.getParameter("id");
        String newCount = request.getParameter("newCount");
        // 2. 从 session 中获取购物车
        HttpSession session = request.getSession();
        Cart cart = (Cart) session.getAttribute("cart");
        // 3. 调用购物车的修改数量方法
        cart.updateItemCount(Integer.valueOf(id), Integer.valueOf(newCount));
        // 4. 检查购物项数量，如果为0，则移除购物车
        if (cart.getTotalCount() == 0) {
            session.removeAttribute("cart");
        }
        // 5. 显示购物车页面
        engine.processTemplate("cart/cart", request, response);
    }

    /**
     * 结算
     *
     * @param request
     * @param response
     * @throws IOException
     */
    public void toCheckOutPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        engine.processTemplate("cart/checkout", request, response);
    }
}
